package com.ybkj.o2o.sales.controller.res;


import com.google.common.base.Preconditions;

import com.ybkj.o2o.sales.common.DataGrid;
import com.ybkj.o2o.sales.common.transport.Error;
import com.ybkj.o2o.sales.common.transport.Result;
import com.ybkj.o2o.sales.entity.ActivityRecord;
import com.ybkj.o2o.sales.manager.ActivityManager;
import com.ybkj.o2o.sales.manager.ActivityRecordManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;



@RestController
@RequestMapping(value = "api/activityrecord")
public class ActivityRecordRes {

  @Autowired
  private ActivityManager activityManager;
  @Autowired
  private ActivityRecordManager activityRecordManager;

  /**
   * 分页查询活动领取记录.
   * 
   * @param activityId 活动id
   * @param companyId 公司id
   * @param phone 手机号
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param page 页数
   * @param rows 每页数量
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "queryByActivity", method = RequestMethod.GET)
  public ResponseEntity<Object> findCoupon(
      @RequestParam(required = true, value = "activityId") Long activityId, @RequestParam(
          required = false, value = "companyId") Long companyId, @RequestParam(required = false,
          value = "phone") String phone,
      @RequestParam(required = false, value = "startTime") Long startTime, @RequestParam(
          required = false, value = "endTime") Long endTime, @RequestParam(required = false,
          value = "page") Integer page,
      @RequestParam(required = false, value = "rows") Integer rows, @RequestParam(required = true,
          value = "appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      Date start = null;
      Date end = null;
      if (startTime != null) {
        start = new Date(startTime);
      }
      if (endTime != null) {
        end = new Date(endTime);
      }
      String passengerPhone = null;
      if (StringUtils.isNotBlank(phone)) {
        passengerPhone = "%" + phone + "%";

      }
      DataGrid<ActivityRecord> activityRecords =
          activityRecordManager.queryByActivity(activityId, companyId, passengerPhone, start, end,
              page, rows, appKey);
      System.out.println("ActivityRecordRes.findCoupon()activityRecords:"
          + activityRecords.getTotal());
      Result<DataGrid<ActivityRecord>> result =
          new Result<DataGrid<ActivityRecord>>(0, "成功", activityRecords);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {

      ex.printStackTrace();

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }

  /**
   * 根据条件统计活动领取记录.
   * 
   * @param activityId 活动id
   * @param companyId 公司id
   * @param phone 手机号
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "countByParam", method = RequestMethod.GET)
  public ResponseEntity<Object> countByParam(
      @RequestParam(required = true, value = "activityId") Long activityId, @RequestParam(
          required = false, value = "companyId") Long companyId, @RequestParam(required = false,
          value = "phone") String phone,
      @RequestParam(required = false, value = "startTime") Long startTime, @RequestParam(
          required = false, value = "endTime") Long endTime, @RequestParam(required = true,
          value = "appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");

    try {
      Date start = null;
      Date end = null;
      if (startTime != null) {
        start = new Date(startTime);
      }
      if (endTime != null) {
        end = new Date(endTime);
      }
      String passengerPhone = null;
      if (StringUtils.isNotBlank(phone)) {
        passengerPhone = "%" + phone + "%";
      }
      Long count =
          activityRecordManager.countActivityRecord(activityId, companyId, passengerPhone, start,
              end, appKey);
      System.out.println("ActivityRecordRes.countActivityRecord()count:" + count);
      Result<Long> result = new Result<Long>(0, "成功", count);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {

      ex.printStackTrace();

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }

  }


  /**
   * 根据客户id和活动id查询活动记录数量.
   * 
   * @param passengerId 客户id
   * @param activityId 活动id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "countActivityRecord", method = RequestMethod.GET)
  public ResponseEntity<Object> countActivityRecord(@RequestParam("passengerId") Long passengerId,
      @RequestParam("activityId") Long activityId, @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);

    try {

      Long times =
          activityRecordManager.countByActivityAndPassenger(activityId, passengerId, appKey);
      Result<Long> result = new Result<Long>(0, "成功", times);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {

      ex.printStackTrace();

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


  /**
   * 根据ID查询单个活动记录.
   * 
   * @param recordId 活动记录id
   * @param appKey 系统key
   * @return ResultTransport{code,message,data}
   */
  @RequestMapping(value = "{recordId}", method = RequestMethod.GET)
  public ResponseEntity<Object> getActivityRecord(@PathVariable("recordId") Long recordId,
      @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);

    ActivityRecord activityRecord = activityRecordManager.findOne(recordId, appKey);
    try {

      Result<ActivityRecord> result = new Result<ActivityRecord>(0, "成功", activityRecord);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;

    } catch (Exception ex) {

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }


}
